<?php
namespace Api\Controller;

//发出请求
class RequestController extends BaseController {

    public $token;

    public function __construct()
    {
        parent::__construct();
        //获取token
        if(empty(I('session.token'))){  //如果没有token，去获取token
            $ret = $this->getToken();
            $ret = json_decode($ret,true);
            if($ret["status"] != 200){  //获取token失败
                echo $ret["error_msg"];;
                exit;
            }else{                      //获取token成功
                $this->token = $ret["token"];
                $_SESSION['token'] = $this->token;
            }
        }else{                          ////如果有token，去验证token
//            $this->token = I('session.token');
            $post_data['token'] = I('session.token');
            $post_data['type'] = "verify_token";


            $ret = $this->post_curl($post_data);

            $ret = json_decode($ret,true);

            if($ret["status"] != 200){  //验证token失败
                $_SESSION['token'] = null;
                $this->__construct();
            }else{                      //验证token成功
                $this->token = I('session.token');
            }
        }
    }

    //执行一个post的curl操作
    public function post_curl($post_data,$url="http://xhsd.yl.sunday.so/api/Receive/verify"){
        $ch = curl_init();                                  //启动一个CURL会话
        curl_setopt ($ch,CURLOPT_URL, $url);// 要访问的地址
        curl_setopt ($ch,CURLOPT_POST, 1);                  //post提交方式
        curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);    //Post提交的数据包
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);          //获取的信息以文件流的形式返回。如果成功只将结果返回，不自动输出任何内容。如果失败返回FALSE
        curl_setopt($ch,CURLOPT_HEADER, 0);                 //显示返回的Header区域内容
//        curl_setopt($ch, CURLOPT_TIMEOUT, 30);              // 设置超时限制防止死循环
        $ret = curl_exec($ch);                            //执行操作
        curl_close($ch);                                    //关闭CURL会话
        return $ret;
    }

    //给出AK、SK，注册token
    public function getToken(){
        $post_data["AK"] = '0235ea76dd6db62be4107320dc9cee2f';
        $post_data["SK"] = '3e2b8172c392db153cd7566414baa038';
        $url = "http://xhsd.yl.sunday.so/api/Receive/register";
        $ret = $this->post_curl($post_data,$url);
        return $ret;
    }

    //读取接口数据
    public function getOne(){
        $post_data['token'] = $this->token;
        $post_data['type'] = "one";
        $post_data['data'] = ["spbs"=>"0843621"];
        $post_data['data'] = json_encode($post_data['data']);

        $ret = $this->post_curl($post_data);

        echo $ret;
    }
    
    //读取接口数据
    public function getAll(){
        $post_data['token'] = $this->token;
        $post_data['type'] = "all";
        $i = 1;
        $all = array();
        do{
            $post_data['data'] = ["p"=>$i];
            $post_data['data'] = json_encode($post_data['data']);

            $ret = $this->post_curl($post_data);
            $ret = json_decode($ret,true);
            if(!empty($ret['data']) && $ret['status']==200){
                $all[] = $ret['data'];
            }
            if($ret['status']!=200){
                echo $ret['error_msg'];
            }
            $i++;
        }while(!empty($ret['data']) || $ret['status']!=200);
        var_dump($all);
    }




}